package servletmvc.imooc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import servletmvc.imooc.entity.User;
import servletmvc.imooc.service.UserService;
import servletmvc.imooc.service.impl.UserServiceImpl;
import servletmvc.imooc.util.DBUtil;
import servletmvc.imooc.util.JSONUtil;

public class UserServlet extends HttpServlet {
	
	//注入service
	UserService userService = new UserServiceImpl();
	
	
	private static final long serialVersionUID = 1L;
       
    public UserServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置返回编码格式,解决中文乱码
		response.setCharacterEncoding("utf-8");
		
		//根据action的值来执行不同的操作,例如显示所有用户或显示单个用户
		String action = request.getParameter("action");
		
		//显示所有用户
		if(action.equals("listAll")) {
			List<User> userList = userService.getUserList();
			//返回json数据格式
			JSONArray jsonArr = JSONArray.fromObject(userList);//转成json数组
			JSONObject jsonObjOut = new JSONObject();//声明json对象
			//将集合转化成json格式
			jsonObjOut.put("userList", jsonArr);
			//调用写好的方法,返回json格式给前台
			JSONUtil.returnJSON(request, response, jsonObjOut);
		}
		
		//显示单个用户
		if(action.equals("getById")) {
			User user = userService.getById(Integer.parseInt(request.getParameter("id")));
			//返回JSON数据格式
			JSONObject jsonObjOut = new JSONObject();
			jsonObjOut.put("user", user);
			JSONUtil.returnJSON(request, response, jsonObjOut);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取JSON数据
		JSONObject jsonObjIn = JSONUtil.getJSON(request, response);
		User user = new User();
		user.setUsername(jsonObjIn.getString("username"));
		user.setPassword(jsonObjIn.getString("password"));
		user.setName(jsonObjIn.getString("name"));
		user.setGender(jsonObjIn.getString("gender"));
		user.setInfo(jsonObjIn.getString("info"));
		
		int effectedNum = userService.addUser(user);
		//返回JSON数据格式
		JSONObject jsonObjOut = new JSONObject();
		jsonObjOut.put("effectedNum", effectedNum);
		JSONUtil.returnJSON(request, response, jsonObjOut);
	}
	
	protected void doDelete(HttpServletRequest request, HttpServletResponse response)
	             throws ServletException, IOException {
		//获取JSON数据
		JSONObject jsonObjIn = JSONUtil.getJSON(request, response);
		int id = jsonObjIn.getInt("id");
		
		int effectedNum = userService.deleteUserById(id);
		//返回JSON数据格式
		JSONObject jsonObjOut = new JSONObject();
		jsonObjOut.put("effectedNum", effectedNum);
		JSONUtil.returnJSON(request, response, jsonObjOut);
	}
	
	protected void doPut(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
		//获取JSON数据格式
		JSONObject jsonObjIn = JSONUtil.getJSON(request, response);
		User user = new User();
		user.setId(jsonObjIn.getInt("id"));
		user.setUsername(jsonObjIn.getString("username"));
		user.setPassword(jsonObjIn.getString("password"));
		user.setName(jsonObjIn.getString("name"));
		user.setGender(jsonObjIn.getString("gender"));
		user.setInfo(jsonObjIn.getString("info"));
		int effectedNum = userService.updateUserById(user);
		//返回json数据格式
		JSONObject jsonObjOut = new JSONObject();
		jsonObjOut.put("effectedNum", effectedNum);
		JSONUtil.returnJSON(request, response, jsonObjOut);
	}

}
